package com.mljr.example.widget.webview;

import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.ctakit.sdk.app.R;
import com.ctakit.sdk.app.base.BaseActivity;
import com.ctakit.sdk.app.service.ActionCallBack;
import com.mljr.example.ui.h5.Html5Activity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhenmin on 14/12/30.
 */

/**
 * 带进度条的WebView
 *
 */
public class ProgressWebView extends WebView {

    private ProgressBar progressbar;
    public static  String keyNewWindow = "newWindow" ;
    protected Map<String,String> titleMap = new HashMap<>();

    protected ActionCallBack<String> actionTitleCallBack;
    protected OnWebLoadListener onWebLoadListener;




    public interface OnWebLoadListener {
        boolean onWebStartLoad(String decodeUrl, String url);
    }

    public void setOnWebLoadListener(OnWebLoadListener onWebLoadListener) {
        this.onWebLoadListener = onWebLoadListener;
    }

    public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
        progressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 3, 0, 0));
        progressbar.setProgressDrawable(context.getResources().getDrawable(R.drawable.webview_progress_color));
        addView(progressbar);
        setWebChromeClient(new WebChromeClient());
    }


    public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
            } else {
                if (progressbar.getVisibility() == GONE)
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if(actionTitleCallBack!=null) {
                titleMap.put(view.getUrl(), title);
                actionTitleCallBack.onSuccess(title);

            }
        }

        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
            WebView newWebView = new WebView(view.getContext());
            newWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                   if(overrideUrlLoading(view, url)){
                       return true;
                   }
                    //跳转到 h5
                    Map<String, Serializable> data = new HashMap<String, Serializable>();
                    data.put("url", url);
                    data.put(keyNewWindow, 1);
                    ((BaseActivity) getContext()).gotoActivity(Html5Activity.class,data);
                    return true;
                }
            });
            WebViewTransport transport = (WebViewTransport) resultMsg.obj;
            transport.setWebView(newWebView);
            resultMsg.sendToTarget();
            return true;
        }
    }

    protected boolean overrideUrlLoading(WebView view, String url){
        return false ;

    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
        lp.x = l;
        lp.y = t;
        progressbar.setLayoutParams(lp);
        super.onScrollChanged(l, t, oldl, oldt);
    }


    public void setActionTitleCallBack(ActionCallBack<String> actionTitleCallBack) {
        this.actionTitleCallBack = actionTitleCallBack;
    }
}